package com.example.utils;

import com.example.bean.ApiResult;

public class ApiResultHandler<T> {

    private Integer code;
    private  String message;
    private T data;

    public ApiResultHandler() {
    }

    public ApiResultHandler(ApiResult apiResult) {

        if(apiResult == null){
            return;
        }
        this.data = (T) apiResult.getData();
        this.code = apiResult.getCode();
        this.message = apiResult.getMessage();

    }

    public static ApiResult success(Object object) {
        ApiResult apiResult = new ApiResult();
        apiResult.setData(object);
        apiResult.setCode(200);
        apiResult.setMessage("请求成功");
        return apiResult;
    }

    public static ApiResult success() {
        return success(null);
    }

    public static <T> ApiResult buildApiResult(Integer code, String message, T data) {
        ApiResult apiResult = new ApiResult();
        apiResult.setCode(code);
        apiResult.setMessage(message);
        apiResult.setData(data);

        return apiResult;
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}
